package edu.hnui.shop.commons;

/**
 * 异步影响的结果对象
 */
public class R {
    /**
     * 标识结果的状态，true:表示成功，false表示失败
     */
    private boolean flag;
    /**
     * 定义的业务编码 400100 表示用户名或者密码错误
     */
    private Integer code;
    /**
     * 响应的消息
     */
    private String message;

    /**
     * 响应给前端的数据
     */
    private Object data;


    public static R ok(){
        R r = new R();
        r.setFlag(ResultCodeEnum.SUCCESS.isFlag());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        return r;
    }

    public static R error(){
        R r = new R();
        r.setFlag(ResultCodeEnum.UNKNOWN_ERROR.isFlag());
        r.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
        r.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
        return r;
    }

    public static R setResult(ResultCodeEnum result){
        R r = new R();
        r.setFlag(result.isFlag());
        r.setMessage(result.getMessage());
        r.setCode(result.getCode());
        return r;
    }

    public R data(Object data){
        this.setData(data);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R message(String message){
        this.setMessage(message);
        return this;
    }



    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
